package com.csii;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import com.csii.adhoc.JdbcTemplate.JdbcTemplateFactory;
import com.csii.adhoc.id.UidGenerator;
import com.csii.adhoc.id.baidu.impl.DefaultUidGenerator;
import com.csii.adhoc.id.baidu.worker.SimpleWorkerIdAssigner;
import lombok.extern.slf4j.Slf4j;
import org.activiti.spring.boot.SecurityAutoConfiguration;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

import javax.annotation.PostConstruct;
import java.time.Duration;

@MapperScan({"com.csii.adhoc.**.mapper","com.csii.pos.**.mapper","com.csii.ams.mapper"})
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
@Slf4j
@EnableFeignClients(basePackages = "com.csii.pos.rpc.upp")
@EnableCaching
public class AdhocRunApp {
    public static void main(String[] args) {
        SpringApplication.run(AdhocRunApp.class,args);
        log.info("-------------------------------------------------------------------------");
        log.info("-----------------------AdhocRunApplication started!!------------------");
        log.info("-------------------------------------------------------------------------");
    }

    @Value("${adhoc.dataSource.size}")
    private Integer size;
    @Bean
    public JdbcTemplateFactory jdbcTemplateFactory(){
        return new JdbcTemplateFactory(size);
    }
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setDbType(DbType.ORACLE);
        // 设置请求的页面大于最大页后操作， true调回到首页，false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量，默认 500 条，-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }

    @Bean
    public UidGenerator uidGenerator(){
        DefaultUidGenerator defaultUidGenerator = new DefaultUidGenerator();
        defaultUidGenerator.setTimeBits(40);
        defaultUidGenerator.setWorkerBits(13);
        defaultUidGenerator.setSeqBits(10);//支持1024并发
        defaultUidGenerator.setEpochStr("2021-01-20");//基准时间
        //目前单机机器码可不用动态生成，后续多机器部署使用DisposableWorkerIdAssigner
        defaultUidGenerator.setWorkerIdAssigner(new SimpleWorkerIdAssigner());
        return defaultUidGenerator;
    }



    @Bean
    public ConcurrentMapCacheManager cacheManager() {
        return new ConcurrentMapCacheManager();
    }
}
